/*!
 * tykhome - app.js
 * Author tykhome <yongketian@gmail.com>
 */

/**
 * Module dependencies.
 */
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var app = express();
var router = require('./routes');

// HTTPS公钥、私钥
var httpsOptions = {
	key: fs.readFileSync('bin/key.pem'),
	cert: fs.readFileSync('bin/key-cert.pem')
};

// configure application 应用配置
require('./config')(app);

// routes 路由配置
app.use('/', router.Home);
app.use('/sign', router.Sign)
app.use('/soft', router.Soft);
app.use('/tag', router.Tag)

// 400 界面
app.use(function(req, res, next) {
	res.render('400');
});

// app.get('*', function(req, res){
//     res.render('404.html', {
//         title: 'No Found'
//     })
// });

// 多进程
// if (cluster.isMaster) {
// 	for (var i = 0; i < numCPUs; i++) {
// 		cluster.fork();
// 	}

// 	cluster.on('exit', function(worker, code, signal) {
// 		console.log('Worker ' + worker.process.pid + ' died.');
// 	});
// } else {
// 	http.createServer(app).listen(app.get('port'), function() {
// 		console.log('Express server listening on port ' + app.get('port'));
// 	});
// }

http.createServer(app).listen(app.get('port'), function() {
	console.log('Express server listening on port ' + app.get('port'));
});

https.createServer(httpsOptions, app).listen(443, function() {
	console.log('Express server listening on port ' + 443);
});